package com.platform.modules.sys.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.platform.config.XlingConstants;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.util.StringUtil;
import com.google.api.client.util.StringUtils;
import com.platform.common.annotation.SysLog;
import com.platform.common.enumstatus.AptitudeEnum;
import com.platform.common.utils.Constant;
import com.platform.common.utils.RestResponse;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.sys.dto.LoginByAppDto;
import com.platform.modules.sys.dto.LoginoutByAppDto;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysCaptchaService;
import com.platform.modules.sys.service.SysOrgService;
import com.platform.modules.sys.service.SysUserService;
import com.platform.modules.sys.service.SysUserTokenService;
import com.platform.modules.sys.vo.LoginByAppVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * APP登录相关
 *
 * @author 表华明
 */
@Api(tags = "SysLoginAppController|用户登录")
@RestController
public class SysLoginAppController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserTokenService sysUserTokenService;
	@Autowired	
	private SysOrgService sysOrgService;


    /**
	 * APP用户登录
	 * @param  登录表单
	 * @return RestResponse
	 */
    @ApiOperation(value="APP用户登录" ,notes = "APP用户登录")
	@SysLog("APP用户登录")
	@PostMapping("/sys/loginByApp")
	public CommonResult<LoginByAppVo> loginByApp(@RequestBody @Validated LoginByAppDto loginByAppDto, HttpServletRequest request) {

    	HttpSession session=request.getSession();
		// 用户信息
		SysUserEntity user = sysUserService.queryByUserName(loginByAppDto.getUserName());

		// 账号不存在、密码错误
		if (user == null || !user.getPassword().equals(new Sha256Hash(loginByAppDto.getPassword(), user.getSalt()).toHex())) {
			CommonResult commonResult = CommonResult.error("账号或密码不正确");
			commonResult.setData(new HashMap());
			return commonResult;
		}
		// 账号锁定
		if (!AptitudeEnum.aptitudeStatus.YBSYPZ.getKey().equals(user.getStatus())) {
			CommonResult commonResult = CommonResult.error("账号已被锁定或未生效,请联系管理员");
			commonResult.setData(new HashMap());
			return commonResult;
		}
		// 生成token，并保存到数据库
		String token = sysUserTokenService.createToken(user.getUserId(), XlingConstants.CLIENT_MOBILE);
		session.setAttribute("user", user);
		
		LoginByAppVo data = new LoginByAppVo();
		data.setUser(user);
		data.setExpire(Constant.EXPIRE);
		data.setToken(token);
		
		//判断登录用户所属机构是不是 项目组织(项目组织的用户orgNo为项目编号,SysOrg是查不到数据的)
		String orgNo = user.getOrgNo();
		if(StringUtil.isNotEmpty(orgNo)) {
			SysOrgEntity sysOrgEntity = sysOrgService.getById(orgNo);
			if(null!=sysOrgEntity) {
				data.setProjectOrgFlag("0");
			}else {
				data.setProjectOrgFlag("1");
			}
		}else {
			data.setProjectOrgFlag("0");
		}
		
		return CommonResult.success(data);
	}

	/**
	 * APP用户退出系统
	 *
	 * @return RestResponse
	 */
    @ApiOperation(value="APP用户退出系统" ,notes = "APP用户退出系统")
	@PostMapping("/sys/logoutByApp")
	public RestResponse logoutByApp(@RequestBody @Validated LoginoutByAppDto loginoutByAppDto) {
		sysUserTokenService.logout(loginoutByAppDto.getUserId(), loginoutByAppDto.getToken());
		return RestResponse.success();
	}
    
}
